import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/LayoutView.vue'
import LoginView from '@/views/LoginView.vue'
import ProductListview from '@/views/ProductListview.vue'
import ProductAddView from '@/views/ProductAddView.vue'
import HomeView from '@/views/HomeView.vue'
import UserManageView from '@/views/UserManageView.vue'
import SwiperListView from '@/views/SwiperListView.vue'
import SwiperAddView from '@/views/SwiperAddView.vue'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: HomeView,
        },
        {
          path: '/prolist',
          name: 'prolist',
          component: ProductListview,
        },
        {
          path: '/proadd',
          name: 'proadd',
          component: ProductAddView,
        },
        {
          path: '/usermanage',
          name: 'usermanage',
          component: UserManageView,
        },
        {
          path: '/swiperlist',
          name: 'swiperlist',
          component: SwiperListView,
        },
        {
          path: '/swiperadd',
          name: 'swiperadd',
          component: SwiperAddView,
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
  ],
})
router.beforeEach((to) => {
  const whiteList = ['/login']
  if (whiteList.includes(to.path)) {
    return true
  } else {
    const token = localStorage.getItem('xx-mall-admin-token')
    if (token == null) {
      ElMessage.warning('请先登录')
      return { path: '/login' }
    } else {
      return true
    }
  }
})
export default router
